package com.qiantu.phone.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.base.BaseDialog;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.activity.BrowserActivitySimple;
import com.qiantu.phone.utils.Constants;
import com.qiantu.phone.widget.textview.QMUISpanTouchFixTextView;
import com.qiantu.phone.widget.textview.QMUITouchableSpan;

/**
 * @author luzx
 */
public class PrivacyAgreementConfirmDialog {

    public static final class Builder
            extends BaseDialog.Builder<SceneIntentDialog.Builder> {

        @Nullable
        private OnConfirmClickListener mOnConfirmClickListener;

        public Builder(Context context) {
            super(context);
            init(context);
        }

        private void init(Context context) {
            View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_privacy_agreement, null, false);
            QMUISpanTouchFixTextView dialogContent = dialogView.findViewById(R.id.dialog_content);
            dialogContent.setText(generateSpannable());
            dialogContent.setMovementMethodDefault();
            TextView btnConfirm = dialogView.findViewById(R.id.btn_confirm);
            TextView btnCancel = dialogView.findViewById(R.id.btn_cancel);
            btnConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (mOnConfirmClickListener != null) {
                        mOnConfirmClickListener.onClick();
                    }
                }
            });
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (context instanceof Activity) {
                        ((Activity) context).finish();
                    }
                }
            });
            setContentView(dialogView);
            setCanceledOnTouchOutside(false);
            setCancelable(false);
        }

        private SpannableString generateSpannable() {
            String userPrivacyAgreementHint = AppApplication.getInstance().getResources().getString(R.string.user_privacy_agreement_hint);
            int textNormalColor = AppApplication.getInstance().getThemeColor(R.attr.themeColor);
            SpannableString spannableInfo = new SpannableString(userPrivacyAgreementHint);
            spannableInfo.setSpan(new QMUITouchableSpan(textNormalColor, 0x4d00a68a,
                    0x00000000, 0x00000000) {
                @Override
                public void onSpanClick(View widget) {
                    BrowserActivitySimple.start(getContext(), "https://sv-center.qiantu-iot.com/privacy/index_boyun.html");
                }
            }, 110, 116, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            spannableInfo.setSpan(new QMUITouchableSpan(textNormalColor, 0x4d00a68a,
                    0x00000000, 0x00000000) {
                @Override
                public void onSpanClick(View widget) {
                    BrowserActivitySimple.start(getContext(), "https://sv-center.qiantu-iot.com/privacy/privacy-policy.html");
                }
            }, 117, 123, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            return spannableInfo;
        }

        public void setOnConfirmClickListener(@Nullable OnConfirmClickListener listener) {
            mOnConfirmClickListener = listener;
        }
    }

    public interface OnConfirmClickListener {
        void onClick();
    }
}
